<?php
//商品模型
namespace Mobile\Model;
use Think\Model;

class goodsguigeModel extends Model{
    
    protected $tableName = 'goodsguige';

    /**
     * 自动完成
     * @var array
     */
    protected $_auto = array(
    );
    

    /*
     * 自动验证
     */
    protected $_validate = array(
     );
    
    
    /*
     * 获取列表,部分已
     */
    public function getGoodsguigeLists($where){
        $res = $this->where($where)->order("ord ASC")->select();
        return $res;
    }
    
    
    /*
     * 获取规格类型
     */
    public function getGuige($where){
        
        $new_res = array();
        $res = $this->where($where)->field('guige_sizename,guige_size,size_id,guige_id')->order("ord ASC")->group('guige_size')->select();
        if($res){
            foreach ($res as $k=>$v){
                if(!empty($v['guige_size'])){
                    
                    $child = $this->where(array("size_id"=>$v['size_id']))->field('color_id')->select();
                    $new_child = array();
                    if($child){
                        foreach ($child as $kk=>$vv){
                            $new_child[] = $vv['color_id'];
                        }
                    }
                    
                    
                    $new_res[$v['guige_sizename']][$v['size_id']] = array(
                        "name" => $v['guige_size'],
                        "child" =>  $new_child,
                        "guige_id" => $v['guige_id'],//没有颜色时候的临时方案
                    );
                }
            }
            
            if(empty($new_res[$v['guige_sizename']])){
                unset($new_res[$v['guige_sizename']]);
            }
        }
        
        $color_res = $this->where($where)->field('guige_colorname,guige_color,color_id')->order("ord ASC")->group('guige_color')->select();
        if($color_res){
            foreach ($color_res as $k=>$v){
                
                if(!empty($v['guige_color'])){
                    
                    $child = $this->where(array("color_id"=>$v['color_id']))->field('size_id')->select();
                    $new_child = array();
                    if($child){
                        foreach ($child as $kk=>$vv){
                            $new_child[] = $vv['size_id'];
                        }
                    }
                    
                    
                    $new_res[$v['guige_colorname']][$v['color_id']] = array(
                        "name" => $v['guige_color'],
                        "child" =>  $new_child,
                      //  "guige_id" => 
                    );
                }

                
            }
            if(empty($new_res[$v['guige_colorname']])){
                unset($new_res[$v['guige_colorname']]);
            }
            
        }
        
        return $new_res;
        
    }
    
    
	
    /*
     * 统计数量
     */
    public function getGoodsguigeCount($where){
        $res = $this->where($where)->count();
        return $res;
    }
    
    /*
     * 添加
     */
    public function addGoodsguige($data){
        $res = $this->add($data);
        return $res;
    }
    
    /*
     * 获取信息
     */
    public function getGoodsguige($where){
        $res = $this->where($where)->find();
        return $res;
    }
	
    /*
     * 编辑信息
     */
    public function editGoodsguige($where,$data){
        return $this->where($where)->save($data);
    }
    
    
}
